#include "MultiPopCoreMT.hpp"


//this funtion is executed in parallel over all the cores??? to swap...to fix..
int MultiPopCoreMT::runPartialGeneration(int number)
	{
	int j;
		for(j=0; j<this->n_cores; j++)
            this->myCores[j]->runGeneration(number);

		//Warning: no mutual excusion in the swap operations.
        if( check_IB_timing() )
            apply_IB();
	}




int MultiPopCoreMT::runGeneration(int num)
{
    int n,i,j;
#ifdef __MULTIPOPCORE_DEBUG__
    static FILE *mpc_tdbg_file=NULL;
    clock_t t0,t1;

    if(mpc_tdbg_file==NULL) mpc_tdbg_file=fopen(__MPC_GENTIME_FILE__,"w");
#endif
        IF_DBG_VERB(printf("*"); fflush(stdout);)

#ifdef __MULTIPOPCORE_DEBUG__
        t0=clock();
#endif
	boost::thread* waiting_list[num];
    for(n=0,i=0; n<num; n++, i++)
    {
		boost::thread* new_thread=new boost::thread(boost::bind(&MultiPopCoreMT::runPartialGeneration, this,1));
		waiting_list[n]=new_thread;
        //runPartialGeneration(n);
    }
	
	for(n=0,i=0;n<num;n++)
	{
		waiting_list[i]->join();
	}
	
	
#ifdef __MULTIPOPCORE_DEBUG__
        t1=clock();
        fprintf(mpc_tdbg_file,"%f\n",(t1-t0)/(float)(CLOCKS_PER_SEC));
        fflush(mpc_tdbg_file);
#endif
    return i++;
}




